package emby.expr;

import emby.EmbyRuntime;
import emby.lang.Block;
import emby.lang.EmbyFunction;
import emby.lang.Scope;
import emby.stmt.Return;
import emby.writer.IEmbyCodifier;

/**
 * The AST representation of a lambda function definition.
 * @author Justin
 */
public class LambdaExpression extends Expression{
	private static final long serialVersionUID = -8895498701298986114L;
	Block body;
	Variable[] params;

	public LambdaExpression(Block _body, Variable... parameters){
		body = _body;
		params = parameters;
		_body.add(Return.retNull);
	}

	@Override
	public Object eval(Scope s) {
		EmbyFunction func =  generateLambda(generateLambdaName(this), s, body, params );
		if(EmbyRuntime.echo){
			emby.util.Print.println("Defining lambda function "+func);
		}

		return func;
	}
	
	public static EmbyFunction generateLambda(String name, Scope def_scope, Block body, Variable... params){
		return new EmbyFunction(name, def_scope, body, params);
	}

	public static String generateLambdaName(Object o){
		return "lambda:0x"+Integer.toHexString((o).hashCode()).toUpperCase();
	}
	
	public void codify(IEmbyCodifier ec){
		ec.lambda(params, body);
	}
}
